package ttOrder.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ttOrder.dao.UserMapper;
import ttOrder.model.Food;
import ttOrder.model.FoodType;
import ttOrder.model.TTUser;
import ttOrder.vo.FoodTypeVO;
import ttOrder.vo.FoodVO;
import ttOrder.vo.TTUserVO;

@Transactional
@Service
public class TTUserService implements ITTUserService{
	@Autowired  
	public UserMapper userMapper;

	
	//根据用户名查出TTUser
	@Override
	public TTUserVO findByUsernameAndPassword(String username) {
		// TODO Auto-generated method stub
		TTUserVO ttUserVO = new TTUserVO();		
		TTUser ttUser =  userMapper.findByUsernameAndPassword(username);
		BeanUtils.copyProperties(ttUser, ttUserVO);			
		return ttUserVO;
	
	}

	//注册(插入一条数据到TTUser)
	public void register(TTUserVO ttUserVO) {
		// TODO Auto-generated method stub
		TTUser ttUser = new TTUser();		
		BeanUtils.copyProperties(ttUserVO, ttUser);
		TTUser user =  userMapper.findByUsernameAndPassword(ttUser.getUserName());
		if(user !=null){
			System.out.println("用户已存在");
		}else{
			userMapper.register(ttUser);
		}
		 
	}
	//根据id显示详细user信息
	@Override
	public TTUserVO getUserById(Integer userId) {
		// TODO Auto-generated method stub
				TTUser entity = userMapper.selectUserById(userId);
				if(entity!=null){
					TTUserVO vo = new TTUserVO();
					BeanUtils.copyProperties(entity, vo);							
					return vo;					
				}
				return null;
	}
	//修改User信息	
	@Override
	public void editUser(TTUserVO ttUserVO) {
		// TODO Auto-generated method stub
		TTUser ttUser = new TTUser();
		BeanUtils.copyProperties(ttUserVO, ttUser);
		userMapper.editUser(ttUser);

	}			
}
